#!/usr/bin/env python
# coding=utf-8
#----------------------------------------------------------------------------
# 文件名：      code.py
# 说明：       循环扫描股票代码,将有效股票代码写入数据
# 作者：       fern
# 创建时间：    2015-4-21
# 版权：       (c) 2015 by fern
# 许可证：      apache license
#----------------------------------------------------------------------------
import mysqlDB
import hashlib,re,sys
from bs4 import BeautifulSoup
from  datetime  import  *
import gzip,urllib2,random,time
import urllib
import StringIO
import json
import socket
import traceback
socket.setdefaulttimeout(10)
reload(sys)
sys.setdefaultencoding('utf-8')

class Code:
    def __init__(self):
        self.db =mysqlDB.Mydb(host='localhost', dataBase='stock', user='root', passwd='123456')
        self.db.dbLoad()

    def forstock(self):
        #00 30 深圳 60上海
        #http://apistore.baidu.com/microservice/stock?stockid=sz002230
        for i in range(0,3000):#2752
            zero=''
            y=4-len(str(i))
            for x in range(0,y):
                zero+='0';
            zero+=str(i)
            stockid='sz00%s' % (zero)
            issave=self.ifadd(stockid)
            if issave:
                print stockid
                url='http://apistore.baidu.com/microservice/stock?stockid=%s' % stockid
                html=self.get_html(url)
                if html:
                    js=json.loads(html)
                    self.add(js)

        for i in range(0,1000):
            zero=''
            y=4-len(str(i))
            for x in range(0,y):
                zero+='0'
            zero+=str(i)
            stockid='sz30%s' % (zero)
            issave=self.ifadd(stockid)
            if issave:
                print stockid
                url='http://apistore.baidu.com/microservice/stock?stockid=%s' % stockid
                html=self.get_html(url)
                if html:
                    js=json.loads(html)
                    self.add(js)

        for i in range(0,6000):
            zero=''
            y=4-len(str(i))
            for x in range(0,y):
                zero+='0';
            zero+=str(i)
            stockid='sh60%s' % (zero)
            issave=self.ifadd(stockid)
            if issave:
                print stockid
                url='http://apistore.baidu.com/microservice/stock?stockid=%s' % stockid
                html=self.get_html(url)
                if html:
                    js=json.loads(html)
                    self.add(js)

    def add(self,js):
        if js['errNum']==0:
            st=js['retData']
            info=st['stockinfo']
            kl=st['klinegraph']
            sql='INSERT INTO `stock`(`code`, `name`, `py`, `currentPrice`, `minurl`, `dayurl`, `weekurl`, `monthurl`) VALUES '
            sql+="('%s','%s',' ','%s','%s','%s','%s','%s')" % (info['code'],info['name'],info['currentPrice'],kl['minurl'],kl['dayurl'],kl['weekurl'],kl['monthurl'])
            self.db.save(sql)
            #print sql
            price='INSERT INTO `price`(`code`, `OpenningPrice`, `closingPrice`, `currentPrice`, `hPrice`, `lPrice`, `competitivePrice`, `auctionPrice`, `totalNumber`, `turnover`, `date`, `time`) VALUES '
            price+="('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (info['code'],info['OpenningPrice'],info['closingPrice'],info['currentPrice'],info['hPrice'],info['lPrice'],info['competitivePrice'],info['auctionPrice'],info['totalNumber'],info['turnover'],info['date'],info['time'])
            self.db.save(price)
            #print price
            print "写入成功%s : %s \n\r" % (info['code'],info['name'])
        else:
            print js['errNum']

    def ifadd(self,code):
        #检查股票代码是否已经添加
        sql="SELECT `code` FROM `stock` WHERE `code`='%s';" % code
        query=self.db.query(sql)
        if query:
            return False
        else:
            return True

    def get_html(self,url,num=0):
        if num==None:
            num=0
        elif num>3:
            return False
        try:
            if url != None:
                page = urllib.urlopen(url)
                html = page.read()
            else:
                html=""
            return html
        except:
            traceback.print_exc()
            if num<3:
                num=num+1
                self.get_html(url,num)

    def close(self):
        self.db.dbClose()

if __name__ == '__main__':
    print "Prepare data. is come soon ..."
    code=Code()
    code.forstock()
    code.close()
